// 登录 和 注册 按钮的 切换
let log = document.querySelector("#log");
let reg = document.querySelector("#reg");
log.onclick = function () {
  log.style.border = "";
  document.querySelector(".login").style.display = "block";
  reg.style.border = "none";
  document.querySelector(".register").style.display = "none";
};
reg.onclick = function () {
  reg.style.border = "";
  document.querySelector(".register").style.display = "block";
  log.style.border = "none";
  document.querySelector(".login").style.display = "none";
};

// 注册功能
let ipts = document.querySelectorAll(".ipt1"); // 登录和注册的输入框
let isUsername = false;
ipts[2].onblur = function () {
  let regz = /^[a-zA-Z]\w{5,17}$/;
  if (regz.test(this.value)) {
    axios.get(DetectAPI, { params: { username: this.value } }).then((res) => {
      console.log(res);
      document.querySelector("#p3").innerHTML = res.data.msg;
      if (res.data.code == 1) {
        isUsername = true;
      } else {
        isUsername = false;
      }
    });
  } else {
    document.querySelector("#p3").innerHTML = "用户名不合法";
  }
};
let isPassword = false;
ipts[3].onblur = function () {
  let regz = /^.{6,}$/;
  if (regz.test(this.value)) {
    document.querySelector("#p4").innerHTML = "格式正确";
    isPassword = true;
  } else {
    isPassword = false;
    document.querySelector("#p4").innerHTML = "格式不正确";
  }
};
let ipts2 = document.querySelectorAll(".ipt2"); // 登录和注册按钮
ipts2[1].onclick = function () {
  if (isUsername && isPassword) {
    let username = ipts[2].value;
    let password = ipts[3].value;
    let params = { username, password };
    axios.get(RegAPI, { params }).then((res) => {
      console.log(res);
      if (res.data.code == 1) {
        confirm("注册成功，是否登录");
        // 用户点确认后，调用登录接口，自动登录
        if (confirm) {
          axios.get(LoginAPI, { params }).then((res) => {
            console.log(res);
            localStorage.setItem("username",username);
            localStorage.setItem("uid",res.data.data.id);
            localStorage.setItem("token",res.data.data.token);
            location.href = "Homepage.html";
          });
        }
      }
    });
  }
};
// 登录功能
ipts[0].onblur = function () {
  let reg = /^[a-zA-Z]\w{5,17}$/;
  if (reg.test(this.value)) {
    isUsername = true;
  } else {
    isUsername = false;
  }
};
ipts[1].onblur = function () {
  let reg = /^.{6,}$/;
  if (reg.test(this.value)) {
    isPassword = true;
  } else {
    isPassword = false;
  }
};
ipts2[0].onclick = function () {
    if (isUsername && isPassword) {
        let username = ipts[0].value;
        let password = ipts[1].value;
        let params = { username, password };
        axios.get(LoginAPI, { params }).then((res) => {
            if(res.data.code == 1 ){
                console.log(res.data.data);
                let { username, id, token } = res.data.data;
                localStorage.setItem("username",username);
                localStorage.setItem("uid",id);
                localStorage.setItem("token",token);
                location.href = "Homepage.html";
                console.log(res.data.msg);
            }else{
                document.querySelector("#p1").innerHTML = "请输入正确的用户名或密码";
            }
          });
    }else{
        document.querySelector("#p1").innerHTML = "请输入正确的用户名或密码";
    }
    ipts[0].onfocus =function(){
        document.querySelector("#p1").innerHTML = ""
    }
};
